<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <div class="comment-box">
          <van-image round fit="cover" :src="item.aut_photo" />
          <div class="content-wrap">
            <div class="name">{{ item.aut_name }}</div>
            <div class="content">{{ item.content }}</div>
            <div class="time-reply">
              <span class="time">{{ item.pubdate | reltTime }}</span>
              <van-button @click="reply(item)" class="reply" size="mini" type="default"
                >回复({{ item.reply_count }})</van-button
              >
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>

    <!-- 发表评论的区域 -->
    <van-cell-group class="published-wrap">
      <van-field
        v-model.trim="cmtValue"
        center
        clearable
        placeholder="请输入评论"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </van-cell-group>

    <!-- 回复面板 -->
    <van-action-sheet v-model="replyShow" title="发表回复">
      <!-- 
          v-show：才是真正显示和隐藏，控制的是display属性，它不会销毁和创建
          v-if：为true时是添加到dom，为false时是移除掉（那就是创建和销毁）
      -->
      <reply v-if="replyShow" :currentItem="currentItem"/>
    </van-action-sheet>
  </div>
</template>

<script>
import { getComments, sendComment } from "@/api/comment";
// 导入组件
import reply from './reply'
export default {
  components:{
    reply
  },
  data() {
    return {
      loading: false,
      finished: false,
      list: [],
      offset: undefined,
      cmtValue: "",
      replyShow: false,
      // 用来保存当前被点的评论数据
      currentItem:{}
    };
  },
  methods: {
    async onLoad() {
      let res = await getComments({
        // a就是代表获取文章的评论
        type: "a",
        source: this.$route.query.id,
        offset: this.offset,
        limit: 10,
      });

      this.offset = res.data.data.last_id;

      this.list.push(...res.data.data.results);

      // 本次加载完毕
      this.loading = false;

      // 如果end_id为null或者end_id == last_id就代表加载完了
      if (!res.data.data.end_id || this.offset == res.data.data.end_id) {
        this.finished = true;
      }
    },

    // 发布的点击事件
    async send() {
      // 非空判断
      if (this.cmtValue == "") return;

      // 发请求去添加评论
      let res = await sendComment({
        target: this.$route.query.id,
        content: this.cmtValue,
      });

      // 返回值里的res.data.data.new_obj
      // 里面有这条评论添加后的所有信息，只要加到list里，就能显示
      // console.log(res)
      // 并且让最新输入的评论要显示在第一行（就要把数据加在数组的第一个）
      this.list.unshift(res.data.data.new_obj);
      // 发表完成把输入内容清空
      this.cmtValue = "";
    },

    // 回复按钮的点击事件
    reply (item) {

      this.replyShow = true
      this.currentItem = item
    }
  },
};
</script>

<style lang="less">
.comment {
  padding-bottom: 49px;
  .comment-box {
    display: flex;
    .van-image {
      width: 30px;
      height: 30px;
    }

    .content-wrap {
      margin-left: 20px;
      flex: 1;
      .name {
        color: #466b9d;
      }

      .time-reply {
        .time {
          color: gray;
          margin-right: 10px;
        }
        .reply {
          font-size: 12px;
        }
      }
    }
  }

  .published-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>